package com.fei.wechat.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fei.wechat.admin.aspect.annotation.AnnoOperateLog;
import com.fei.wechat.common.constant.OperateLogConstant;
import com.fei.wechat.common.constant.OperateLogContentTemplate;
import com.fei.wechat.common.model.vo.RespVo;
import com.fei.wechat.middlewar.api.admin.IOperateLogService;
import com.fei.wechat.middlewar.api.admin.IOrgaService;
import com.fei.wechat.middlewar.mode.bo.admin.OrgaBo;
import com.fei.wechat.middlewar.mode.entity.admin.Orga;
import com.fei.wechat.middlewar.mode.vo.admin.CurrentUser;

/**
 * 描述: 机构管理
 * 版权: Copyright (c) 2018 
 * 公司: 飞翔科技 
 * 作者: 张进飞 
 * 版本: 1.0 
 * 创建日期: 2018年6月16日 
 * 创建时间: 下午3:10:36
 */
@RequestMapping("/admin/orga")
@Controller
public class OrgaController extends BaseController
{
    
    @Autowired
    private IOrgaService orgaService;
    
    @Autowired
    private IOperateLogService operateLogService;
    
    /**
     * 描述：机构管理index页面
     * 作者：张进飞
     * 时间：2018年6月16日 下午3:12:25
     * @param request1
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("base/org/list");
        return mav;
    }
    
    /**
     * 描述：添加页面
     * 作者：张进飞
     * 时间：2018年6月17日 上午8:36:21
     * @param request1
     * @return
     */
    @RequestMapping("/addPage")
    public ModelAndView addPage(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("base/org/add");
        return mav;
    }
    
    /**
     * 描述：编辑页面
     * 作者：张进飞
     * 时间：2018年6月17日 上午8:36:47
     * @param request1
     * @return
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("base/org/edit");
        return mav;
    }
    
    /**
     * 描述：展示树页面
     * 作者：张进飞
     * 时间：2018年6月17日 上午8:37:21
     * @param request1
     * @return
     */
    @RequestMapping("/treePage")
    public ModelAndView treePage(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("base/org/tree");
        return mav;
    }
    
    /**
     * 描述：查询机构列表
     * 作者：张进飞
     * 时间：2018年6月16日 下午9:26:29
     * @return
     */
    
    @RequestMapping("/queryOrgaAll")
    @ResponseBody
    public List<Orga> queryOrgaAll(HttpServletRequest request)
    {
        List<Orga> list = null;
        CurrentUser currentUser = this.getCurrentUser(request);
        String loginName = currentUser.getUser().getLoginName();
        String operateIP = getLoginIP(request);
        Object[] objArray = new Object[2];
        objArray[0] = loginName;
        try
        {
            list = orgaService.queryOrgaList();
            objArray[1] = "成功";
            operateLogService.saveOperateLog(OperateLogConstant.MODULE_SYSMGR, OperateLogConstant.FUNCTION_PAGE_ORGA, loginName, operateIP, OperateLogConstant.OPERATE_ID_QUERY,
                    OperateLogContentTemplate.QUERY_ORGA, objArray, null);
        }
        catch (Exception e)
        {
            objArray[1] = "失败";
            operateLogService.saveOperateLog(OperateLogConstant.MODULE_SYSMGR, OperateLogConstant.FUNCTION_PAGE_ORGA, loginName, operateIP, OperateLogConstant.OPERATE_ID_QUERY,
                    OperateLogContentTemplate.QUERY_ORGA, objArray, null);
            logger.error("查询机构列表，异常：", e);
        }
        return list;
    }
    
    /**
     * 描述：查询机构列表
     * 作者：张进飞
     * 时间：2018年6月30日 下午10:39:46
     * @param request
     * @return
     */
    @RequestMapping("/queryOrgaList")
    @AnnoOperateLog(moduleName = OperateLogConstant.MODULE_SYSMGR, function = OperateLogConstant.FUNCTION_PAGE_ORGA, operateType = OperateLogConstant.OPERATE_ID_QUERY, operateContentTemplate = OperateLogContentTemplate.QUERY_ORGA)
    @ResponseBody
    public RespVo queryOrgaList(HttpServletRequest request)
    {
        RespVo respVo = new RespVo();
        List<Orga> list = null;
        Object[] objArray = new Object[2];
        CurrentUser currentUser = this.getCurrentUser(request);
        String loginName = currentUser.getUser().getLoginName();
        objArray[0] = loginName;
        try
        {
            list = orgaService.queryOrgaList();
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("查询成功");
            respVo.setResult(list);
            objArray[1] = "成功";
        }
        catch (Exception e)
        {
            logger.error("查询机构列表，异常：", e);
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("查询失败");
            objArray[1] = "失败";
        }
        respVo.setLogArgs(objArray);
        return respVo;
    }
    
    /**
     * 描述：查询机构树
     * 作者：张进飞
     * 时间：2018年6月17日 上午8:42:17
     * @return
     */
    @RequestMapping("/listOrgTree")
    @ResponseBody
    public List<Orga> listOrgTree()
    {
        List<Orga> list = null;
        try
        {
            list = orgaService.listOrgTree();
        }
        catch (Exception e)
        {
            logger.error("查询机构列表，异常：", e);
        }
        return list;
    }
    
    /**
     * 描述：根据id查询
     * 作者：张进飞
     * 时间：2018年6月30日 下午2:00:43
     * @param id
     * @return
     */
    @RequestMapping("/queryOrgaById")
    @ResponseBody
    public RespVo queryOrgaById(@RequestBody Long orgId)
    {
        RespVo respVo = new RespVo();
        try
        {
            Orga orga = this.orgaService.queryOrgaById(orgId);
            respVo.setResult(orga);
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("查询成功");
        }
        catch (Exception e)
        {
            logger.error("根据id，查询机构信息，异常：", e);
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("查询失败");
        }
        return respVo;
    }
    
    /**
     * 描述：保存机构
     * 作者：张进飞
     * 时间：2018年6月17日 上午11:15:05
     * @param orgaBo
     * @return
     */
    @AnnoOperateLog(moduleName = OperateLogConstant.MODULE_SYSMGR, function = OperateLogConstant.FUNCTION_PAGE_ORGA, operateType = OperateLogConstant.OPERATE_ID_ADD, operateContentTemplate = OperateLogContentTemplate.ADD_ORGA)
    @RequestMapping("/saveOrga")
    @ResponseBody
    public RespVo saveOrga(HttpServletRequest request, @RequestBody OrgaBo org)
    {
        RespVo respVo = new RespVo();
        Object[] objArray = new Object[3];
        CurrentUser currentUser = this.getCurrentUser(request);
        String loginName = currentUser.getUser().getLoginName();
        objArray[0] = loginName;
        objArray[1] = org.getOrgaName();
        try
        {
            this.orgaService.saveOrga(org);
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("保存机构成功");
            objArray[2] = "成功";
        }
        catch (Exception e)
        {
            logger.error("保存机构，异常：", e);
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("保存机构失败");
            objArray[2] = "失败";
        }
        respVo.setLogArgs(objArray);
        return respVo;
    }
    
    /**
     * 描述：修改机构
     * 作者：张进飞
     * 时间：2018年6月17日 上午11:16:13
     * @param orgaBo
     * @return
     */
    @AnnoOperateLog(moduleName = OperateLogConstant.MODULE_SYSMGR, function = OperateLogConstant.FUNCTION_PAGE_ORGA, operateType = OperateLogConstant.OPERATE_ID_EDIT, operateContentTemplate = OperateLogContentTemplate.EDIT_ORGA)
    @RequestMapping("/updateOrga")
    @ResponseBody
    public RespVo updateOrga(HttpServletRequest request, @RequestBody OrgaBo org)
    {
        RespVo respVo = new RespVo();
        Object[] objArray = new Object[3];
        CurrentUser currentUser = this.getCurrentUser(request);
        String loginName = currentUser.getUser().getLoginName();
        objArray[0] = loginName;
        objArray[1] = org.getOrgaName();
        try
        {
            this.orgaService.updateOrga(org);
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("更新机构成功");
            objArray[2] = "成功";
        }
        catch (Exception e)
        {
            logger.error("更新机构，异常：", e);
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("更新机构失败");
            objArray[2] = "失败";
        }
        respVo.setLogArgs(objArray);
        return respVo;
    }
    
    /**
     * 描述：删除机构
     * 作者：张进飞
     * 时间：2018年6月17日 上午11:15:58
     * @param orgaBo
     * @return
     */
    @AnnoOperateLog(moduleName = OperateLogConstant.MODULE_SYSMGR, function = OperateLogConstant.FUNCTION_PAGE_ORGA, operateType = OperateLogConstant.OPERATE_ID_DELETE, operateContentTemplate = OperateLogContentTemplate.DELETE_ORGA)
    @RequestMapping("/removeOrga")
    @ResponseBody
    public RespVo removeOrga(HttpServletRequest request, @RequestBody List<Long> ids)
    {
        RespVo respVo = new RespVo();
        Object[] objArray = new Object[3];
        CurrentUser currentUser = this.getCurrentUser(request);
        String loginName = currentUser.getUser().getLoginName();
        try
        {
            objArray[0] = loginName;
            List<Orga> orgaList = this.orgaService.queryOrgaByIds(ids);
            StringBuilder builder = new StringBuilder();
            for (Orga orga : orgaList)
            {
                if (StringUtils.isNotBlank(orga.getOrgaName()))
                {
                    builder.append(orga.getOrgaName()).append(",");
                }
            }
            objArray[1] = StringUtils.isNotBlank(builder.toString()) ? builder.toString().substring(0, builder.toString().length() - 1) : "";
            this.orgaService.removeOrga(ids);
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("删除机构成功");
            objArray[2] = "成功";
        }
        catch (Exception e)
        {
            logger.error("删除机构，异常：", e);
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("删除机构失败");
            objArray[2] = "失败";
        }
        respVo.setLogArgs(objArray);
        return respVo;
    }
}
